package ex3.board;


import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ex3.reversi.GameManager;
import ex3.reversi.ReversiGame;
/**
 * This class represents the visual board.
 * 
 * @author Maxim
 *
 */
public class GamePanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton [][] _buttons;
	
	private JLabel _gameDetails;
	private JLabel _turn;
	private JLabel _score;
	private JLabel _player1Score;
	private JLabel _player2Score;
	
	private GameManager _gameManager;
	private JFrame _mainScreen;
	
	public GamePanel(int gameMode, int startingPlayer, JFrame mainScreen){
		super();
		initializeLabels();
		this._mainScreen = mainScreen;
		this._gameManager = new GameManager(gameMode,startingPlayer);

		JPanel tButtons = new JPanel( new GridLayout(ConfigClass.BOARD_SIZE, ConfigClass.BOARD_SIZE));
	
		JButton b;
		
		this._buttons = new JButton[ConfigClass.BOARD_SIZE][ConfigClass.BOARD_SIZE];
		for (int i = 0; i < ConfigClass.BOARD_SIZE; i++)
			for(int j = 0; j < ConfigClass.BOARD_SIZE; j++){

				b = new JButton();
				b.setBackground(Color.ORANGE);
				b.addActionListener(new ButtonPressed(i, j, this));
				tButtons.add(b);
				this._buttons[i][j] = b;
			}
    
		updateVisualBoard(this._gameManager.get_board());
		updateResults();
		this.add(tButtons);
		
		GroupLayout tBodyLayout = new  GroupLayout(this);
		setHorizontalGroup(tBodyLayout, tButtons);
		setVerticalGroup(tBodyLayout, tButtons);	
	}
	
	public void buttonPressed(int i, int j){
		
		this._gameManager.move(i, j);
		updateVisualBoard(this._gameManager.get_board());
		updateResults();
		
		if(this._gameManager.is_isGameOver()){
			if(this._gameManager.get_player1Score() > this._gameManager.get_player2Score()){
				 JOptionPane.showMessageDialog(null, "Player 1 win!!!", "Game Over", 1);
				 this._mainScreen.dispose();
				 new ReversiGame();
			}
			else{
				if(this._gameManager.get_player1Score() > this._gameManager.get_player2Score()){
					JOptionPane.showMessageDialog(null, "Player 2 win!!!", "Game Over", 1);
					this._mainScreen.dispose();
					new ReversiGame();
				}
				else{
					 JOptionPane.showMessageDialog(null, "Tie!!!", "Game Over", 1);
					 this._mainScreen.dispose();
					 new ReversiGame();
				}
			}

		}
		

		
	}
	
	
	private void updateVisualBoard(int[][] gameBoard){
		
		for(int i = 0; i < ConfigClass.BOARD_SIZE; i++ )
			
			for(int j = 0; j < ConfigClass.BOARD_SIZE;j++ ){
				
				switch(gameBoard[i][j]){
				
					case ConfigClass.EMPTY_CELL:
						this._buttons[i][j].setBackground(Color.ORANGE);
						break;
					
					case ConfigClass.WHITE_PIECE:
						this._buttons[i][j].setBackground(Color.WHITE);
						break;
						
					case ConfigClass.BLACK_PIECE:
						this._buttons[i][j].setBackground(Color.BLACK);
						break;	
				}	
			}
	}
	
	private void updateResults(){
		
		this._turn.setText(ConfigClass.TURN_TITLE + this._gameManager.get_turn());
		this._player1Score.setText(ConfigClass.PLAYER_1_SCORE_TITLE+ " " +this._gameManager.get_player1Score());
		this._player2Score.setText(ConfigClass.PLAYER_2_SCORE_TITLE+ " " +this._gameManager.get_player2Score());
	}
	
	
	private void initializeLabels(){
		this._gameDetails = new JLabel(ConfigClass.GAME_DETAILS_TITLE);
		this._turn = new JLabel(ConfigClass.TURN_TITLE);
		this._score = new JLabel(ConfigClass.SCORE_TITLE);
		this._player1Score = new JLabel(ConfigClass.PLAYER_1_SCORE_TITLE);
		this._player2Score = new JLabel(ConfigClass.PLAYER_2_SCORE_TITLE);
		
	}
	
	
	private void setHorizontalGroup(GroupLayout BodyLayout, JPanel Buttons){
		
		this.setLayout(BodyLayout);
		BodyLayout.setHorizontalGroup(BodyLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addGroup(BodyLayout.createSequentialGroup()
						.addComponent(Buttons, GroupLayout.PREFERRED_SIZE, 486, GroupLayout.PREFERRED_SIZE)
						.addGroup(BodyLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addGroup(BodyLayout.createSequentialGroup()
										.addGap(18,18,18)
										.addGroup(BodyLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
												.addComponent(this._gameDetails)
												.addComponent(this._turn)
												.addComponent(this._score)))
												.addGroup(BodyLayout.createSequentialGroup()
														.addGap(28,28,28)
														.addGroup(BodyLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
																.addComponent(this._player1Score)
																.addComponent(this._player2Score))))
																.addContainerGap(89,Short.MAX_VALUE)));
		
	}
	
	private void setVerticalGroup(GroupLayout BodyLayout, JPanel Buttons){
		
		BodyLayout.setVerticalGroup(
				BodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(Buttons, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                .addGroup(BodyLayout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addComponent(this._gameDetails)
                    .addGap(18, 18, 18)
                    .addComponent(this._turn)
                    .addGap(18, 18, 18)
                    .addComponent(this._score)
                    .addGap(18, 18, 18)
                    .addComponent(this._player1Score)
                    .addGap(28, 28, 28)
                    .addComponent(this._player2Score)
                    .addGap(28, 28, 28)
                    
                    .addContainerGap(215, Short.MAX_VALUE))
            );
		
	}

}
